//
//  ZZHomeTextViewCell.swift
//  QMXK
//
//  Created by jenkin on 2018/12/5.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit
import AlamofireImage

class ZZHomeTextViewCell: ZZBaseCell {

    var baseModel:ZZArticleModel!
    var fromLabel:UILabel!
    var timeLabel:UILabel!
    var titleLabel:UILabel!
    var line:UIView!
    let rate:CGFloat = CommenConst.shareCommentConst.KSIZE(1)
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    func setUI(){
        let titleFont:UIFont = CommenConst.shareCommentConst.KNFONT(rate * 18)
        let blackColor = ColorConst.shareColorConst.titleColor()
        let textColor = ColorConst.shareColorConst.lightTextColor()
        
        self.titleLabel = ZZViewUtil.shareViewUtil.createLabel(textAlign: NSTextAlignment.left, text: "", font: titleFont, color: blackColor, autoLine: true)
        self.addSubview(self.titleLabel)
        
        let fromFont:UIFont = CommenConst.shareCommentConst.KNFONT(rate * 12)
        self.fromLabel = ZZViewUtil.shareViewUtil.createLabel(textAlign: NSTextAlignment.left, text: "", font: fromFont, color: textColor, autoLine: false)
        self.addSubview(self.fromLabel)
        
        self.timeLabel = ZZViewUtil.shareViewUtil.createLabel(textAlign: NSTextAlignment.left, text: "", font: fromFont, color: textColor, autoLine: false)
        self.addSubview(self.timeLabel)
        
        self.line = ZZViewUtil.shareViewUtil.createLineView()
        self.addSubview(self.line)
    }
    
    func refreshModel(){
        self.titleLabel.text = self.baseModel?.title
        self.fromLabel.text = self.baseModel?.from
        self.timeLabel.text = self.baseModel?.day
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// 纯标题
class ZZHomeTextTitleCell : ZZHomeTextViewCell{
    
    var model:ZZArticleModel?{
        get{
            return self.baseModel
        }
        set{
            self.baseModel = newValue
            self.refreshModel()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    override func setUI(){
        super.setUI()
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(rate * 12)
            make.right.equalTo(rate * -12)
            make.top.equalTo(rate * 12)
        }
        
        self.fromLabel.snp.makeConstraints { (make) in
            make.left.equalTo(rate * 12)
            make.height.equalTo(self.rate * 12)
            make.top.equalTo(self.titleLabel.snp.bottom).offset(self.rate * 18)
        }
        
        self.timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.fromLabel.snp.right).offset(self.rate * 12)
            make.height.equalTo(self.rate * 12)
            make.centerY.equalTo(self.fromLabel)
        }
        
        self.line.snp.makeConstraints { (make) in
            make.top.equalTo(self.timeLabel.snp.bottom).offset(self.rate * 12)
            make.height.equalTo(self.rate)
            make.left.equalTo(self.rate * 12)
            make.right.equalTo(self.rate * -12)
            make.bottom.equalTo(0)
        }
    }
    
    // 刷新数据
    override func refreshModel() {
        super.refreshModel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ZZHomeTextTitleAndImageCell : ZZHomeTextViewCell{
    
    var img1:UIImageView!
    var img2:UIImageView!
    var img3:UIImageView!
    var model:ZZArticleModel?{
        get{
            return self.baseModel
        }
        set{
            self.baseModel = newValue
            self.refreshModel()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    override func setUI(){
        super.setUI()
        
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(rate * 12)
            make.right.equalTo(rate * -12)
            make.top.equalTo(rate * 12)
        }
        
        self.img1 = ZZViewUtil.shareViewUtil.createImageView(contentmodel: UIView.ContentMode.scaleToFill, name: "")
        self.addSubview(self.img1)
        self.img2 = ZZViewUtil.shareViewUtil.createImageView(contentmodel: UIView.ContentMode.scaleToFill, name: "")
        self.addSubview(self.img2)
        self.img3 = ZZViewUtil.shareViewUtil.createImageView(contentmodel: UIView.ContentMode.scaleToFill, name: "")
        self.addSubview(self.img3)
        
        let imgArray:Array<UIImageView> = [self.img1, self.img2, self.img3]
        let count = imgArray.count
        let width = self.rate * 114
        let height = self.rate * 79
        let x0 = self.rate * 12
        let spaceW = CommenConst.KScreenW - width * CGFloat(count) - 2 * x0
        let space = spaceW / CGFloat(count - 1)
        let itemW = (width + space)
        
        for index in 0..<count{
            let img:UIImageView = imgArray[index]
            img.snp.makeConstraints { (make) in
                make.left.equalTo(x0 + CGFloat(index) * itemW)
                make.height.equalTo(height)
                make.width.equalTo(width)
                make.top.equalTo(self.titleLabel.snp.bottom).offset(self.rate * 18)
            }
        }
        
        self.fromLabel.snp.makeConstraints { (make) in
            make.left.equalTo(rate * 12)
            make.height.equalTo(self.rate * 12)
            make.top.equalTo(self.img1.snp.bottom).offset(self.rate * 14)
        }
        
        self.timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.fromLabel.snp.right).offset(self.rate * 12)
            make.height.equalTo(self.rate * 12)
            make.centerY.equalTo(self.fromLabel)
        }
        
        self.line.snp.makeConstraints { (make) in
            make.top.equalTo(self.timeLabel.snp.bottom).offset(self.rate * 12)
            make.height.equalTo(self.rate)
            make.left.equalTo(self.rate * 12)
            make.right.equalTo(self.rate * -12)
            make.bottom.equalTo(0)
        }
    }
    
    override func refreshModel(){
        super.refreshModel()
        
        ZZBaseUtil.shareUtil.setImageView(imageView: self.img1, url: self.baseModel.titlepic1 ?? "")
        ZZBaseUtil.shareUtil.setImageView(imageView: self.img2, url: self.baseModel.titlepic2 ?? "")
        ZZBaseUtil.shareUtil.setImageView(imageView: self.img3, url: self.baseModel.titlepic3 ?? "")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ZZHomeTextLeftTitleAndImageCell : ZZHomeTextViewCell{

    var pic:UIImageView!
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    var model:ZZArticleModel?{
        get{
            return self.baseModel
        }
        set{
            self.baseModel = newValue
            self.refreshModel()
        }
    }
    
    override func setUI(){
        super.setUI()
        self.pic = ZZViewUtil.shareViewUtil.createImageView(contentmodel: UIView.ContentMode.scaleToFill, name: "")
        self.addSubview(self.pic)
        self.pic.snp.makeConstraints { (make) in
            make.right.equalTo(-self.rate * 12)
            make.width.equalTo(self.rate * 114)
            make.height.equalTo(self.rate * 79)
            make.top.equalTo(self.rate * 12)
        }
        
        self.titleLabel.numberOfLines = 2
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.rate * 12)
            make.top.equalTo(self.rate * 12)
            make.right.equalTo(self.pic.snp.left).offset(-self.rate * 8)
        }
        
        self.fromLabel.snp.makeConstraints { (make) in
            make.left.equalTo(rate * 12)
            make.height.equalTo(self.rate * 12)
            make.bottom.equalTo(self.pic.snp.bottom)
        }
        
        self.timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.fromLabel.snp.right).offset(self.rate * 12)
            make.height.equalTo(self.rate * 12)
            make.centerY.equalTo(self.fromLabel)
        }
        
        self.line.snp.makeConstraints { (make) in
            make.top.equalTo(self.timeLabel.snp.bottom).offset(self.rate * 12)
            make.height.equalTo(self.rate)
            make.left.equalTo(self.rate * 12)
            make.right.equalTo(self.rate * -12)
            make.bottom.equalTo(0)
        }
    }
    
    override func refreshModel() {
        super.refreshModel()
        ZZBaseUtil.shareUtil.setImageView(imageView: self.pic, url: self.baseModel.pic)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
